<?php

namespace Core\process;

use Core\helper\FilesHelper;
use Swoole\Process;

class FilesProcess
{
    private $files_md5;

    public function run()
    {
        return new Process(function () {
            while (true) {
                sleep(3);
                //根据文件MD5判断是否修改,修改的话发送信号 重启
                $files_md5 = FilesHelper::getFilesMd5(ROOT_PATH . "/app", "app/config");
                if (!$this->files_md5) {
                    $this->files_md5 = $files_md5;
                    continue;
                }
                if (strcmp($this->files_md5, $files_md5) !== 0) {
                    echo "reloading..." . PHP_EOL;
                    //获取master 进程pid
                    $pid = file_get_contents(ROOT_PATH . "/wolf.pid");
                    //重启
                    Process::kill($pid, SIGUSR1);
                    echo "reload end" . PHP_EOL;
                    $this->files_md5 = $files_md5;
                }
            }
        });
    }

}
